package com.zhengshang.third.pinduoduo;

import com.pdd.pop.sdk.common.util.JsonUtil;
import com.pdd.pop.sdk.http.PopBaseHttpResponse;
import com.pdd.pop.sdk.http.PopClient;
import com.pdd.pop.sdk.http.PopHttpClient;
import com.pdd.pop.sdk.http.api.pop.request.PddDdkGoodsSearchRequest;
import com.pdd.pop.sdk.http.api.pop.request.PddDdkGoodsZsUnitUrlGenRequest;
import com.pdd.pop.sdk.http.api.pop.response.PddDdkGoodsSearchResponse;
import com.pdd.pop.sdk.http.api.pop.response.PddDdkGoodsZsUnitUrlGenResponse;
import com.zhengshang.common.core.domain.AjaxResult;
import com.zhengshang.common.core.redis.RedisCache;
import com.zhengshang.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 拼多多工具类
 */
@Component
public class PinDuoDuoUtils {

    /**
     * 授权Url
     * response_type 授权步骤类型，固定值为code
     * 拼接示例：https://fuwu.pinduoduo.com/service-market/auth?response_type=code&client_id=4b6&redirect_uri=https%3A%2F%2Fwww.oauth.net%2F2%2F&state=1212
     */
    private static final String oauthUrl = "https://mai.pinduoduo.com/h5-login.html";
    /**
     * api请求地址
     */
    private static final String apiUrl = "http://gw.api.taobao.com/router/rest";

    /**
     * clientId
     */
    public static final String clientId = "7c8c273d76484e06abcedf6844ef451d";
    /**
     * 拼多多密钥
     */
    public static final String clientSecret = "f8cf26715fc67ed12630b219dac35fbc";


    @Autowired
    private RedisCache redisCache;

    /**
     * 获取授权Url
     *
     * @param redirectUri 回调地址参数
     * @param view        可选web或h5
     * @return
     */
    private static String getOauthUrl(String redirectUri, String state, String view) {
        String text = oauthUrl + "?response_type=code&client_id=%s&redirect_uri=%s&state=%s&view=%s";
        String content = String.format(text, clientId, redirectUri, state, view);
        return content;
    }

    /**
     * OauthUrl
     *
     * @param redirectUri
     * @return
     */
    public static String getAppOauthUrl(String redirectUri, String state) {
        String oauthUrl = getOauthUrl(redirectUri, state, "h5");
        return oauthUrl;
    }


    /**
     * 商品搜索
     *
     * @return
     */
    public static AjaxResult goodsSearch(String goodsName, Integer page, Integer sortType, Boolean withCoupon) {
        PopClient client = new PopHttpClient(clientId, clientSecret);
        PddDdkGoodsSearchRequest request = new PddDdkGoodsSearchRequest();
        List<Integer> activityTags = new ArrayList<>();
        activityTags.add(0);
        request.setActivityTags(activityTags);
        List<Integer> blockCatPackages = new ArrayList<>();
        blockCatPackages.add(0);
        request.setBlockCatPackages(blockCatPackages);
        List<Integer> blockCats = new ArrayList<>();
        blockCats.add(0);
        request.setBlockCats(blockCats);
        request.setCatId(49L);
        //设置自定义参数
        request.setCustomParameters("");
        request.setGoodsImgType(1);
        List<String> goodsSignList = new ArrayList<>();
        request.setGoodsSignList(goodsSignList);
        request.setIsBrandGoods(true);
        request.setKeyword(goodsName);
        request.setMerchantType(1);
        List<Integer> merchantTypeList = new ArrayList<>();
        merchantTypeList.add(0);
        request.setMerchantTypeList(merchantTypeList);
        request.setOptId(49L);
        request.setPage(page);
        request.setPageSize(10);
        request.setPid("16055417_206080412");
        request.setSortType(sortType);
        request.setWithCoupon(withCoupon);
        List<PddDdkGoodsSearchRequest.RangeListItem> rangeList = new ArrayList<>();

        PddDdkGoodsSearchRequest.RangeListItem item = new PddDdkGoodsSearchRequest.RangeListItem();
        item.setRangeFrom(0L);
        item.setRangeId(0);
        item.setRangeTo(0L);
        rangeList.add(item);
        request.setRangeList(rangeList);
        request.setSortType(0);
        request.setUseCustomized(true);
        request.setWithCoupon(true);
        try {
            PddDdkGoodsSearchResponse response = client.syncInvoke(request);
            System.out.println(JsonUtil.transferToJson(response));
            PopBaseHttpResponse.ErrorResponse errorResponse = response.getErrorResponse();
            if (StringUtils.isNotNull(errorResponse)) {
                return AjaxResult.success("拼多多接口错误信息：" + errorResponse.getErrorMsg());
            }
            return AjaxResult.success(response.getGoodsSearchResponse().getGoodsList());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    public static String generateGoodsUrl(String sourceUrl) throws Exception {
        PopClient client = new PopHttpClient(clientId, clientSecret);
        PddDdkGoodsZsUnitUrlGenRequest request = new PddDdkGoodsZsUnitUrlGenRequest();
        request.setCustomParameters("str");
        request.setPid("str");
        request.setSourceUrl("str");
        PddDdkGoodsZsUnitUrlGenResponse response = client.syncInvoke(request);
        System.out.println(JsonUtil.transferToJson(response));
        return JsonUtil.transferToJson(response);
    }


    public static void main(String[] args) {
        //String appOauthUrl = getAppOauthUrl("https://www.baidu.com/", "");
        //System.out.println(appOauthUrl);
        goodsSearch("男装", 1, 0, true);
    }
}
